package com.qf.smsplatform.security;
/*2021/11/1*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.smsplatform.dto.R;
import com.qf.smsplatform.mapper.TMenuMapper;
import com.qf.smsplatform.mapper.TRoleMenuMapper;
import com.qf.smsplatform.mapper.TUserRoleMapper;
import com.qf.smsplatform.pojo.MyAuthUser;
import com.qf.smsplatform.pojo.TMenu;
import com.qf.smsplatform.pojo.TRoleMenu;
import com.qf.smsplatform.pojo.TUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Component
public class MySueecssSecurityHandler extends SimpleUrlAuthenticationSuccessHandler {

    private TMenuMapper tMenuMapper;

    private ObjectMapper objectMapper;

    private TUserRoleMapper tUserRoleMapper;

    private TRoleMenuMapper tRoleMenuMapper;

    @Autowired
    public void settUserRoleMapper(TUserRoleMapper tUserRoleMapper) {
        this.tUserRoleMapper = tUserRoleMapper;
    }
    @Autowired
    public void settRoleMenuMapper(TRoleMenuMapper tRoleMenuMapper) {
        this.tRoleMenuMapper = tRoleMenuMapper;
    }

    @Autowired
    public void settMenuMapper(TMenuMapper tMenuMapper) {
        this.tMenuMapper = tMenuMapper;
    }
    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        super.onAuthenticationSuccess(request, response, authentication);
        System.out.println("用户登录成功了");

        //获取当前登录用户的权限
        Object principal = authentication.getPrincipal();
        //获取当前登录用户的id
        Long id = ((MyAuthUser) principal).getId();

        //获取当前用户的所有权限
        //1.根据用户id获取所有的角色id
        List<TUserRole> tUserRoles = tUserRoleMapper.selectRidByUid(id);
        List<Long> rIds = tUserRoles.stream()//一个用户可能有多个角色
                .map(TUserRole::getRoleId)
                .collect(Collectors.toList());
        //2.根据角色id获取所有的地址id
        List<TRoleMenu> tRoleMenus = tRoleMenuMapper.selectMenuIdByRids(rIds);
        //将地址id存入集合当中，进行地址信息的查询
        List<Long> mIds = tRoleMenus.stream()//一个角色可能拥有多个地址id
                .map(TRoleMenu::getMenuId)
                .collect(Collectors.toList());

        //3.根据地址的id获取到所有的地址的信息
        List<TMenu> tMenus = tMenuMapper.selectMenuByMids(mIds);


        List<SimpleGrantedAuthority> collect = tMenus.stream().filter(menu -> !ObjectUtils.isEmpty(menu.getPerms()))//只保留有权限的数据
                .map(TMenu::getPerms)//将当前集合中符合我们要求的数据通过getperms转成字符串集合
                .map(SimpleGrantedAuthority::new)//将字符串结合遍历通过new SimpleGrantedAuthority将权限设置转化为权限对象
                .collect(Collectors.toList());

//        System.out.println("authorityList::::::"+collect);

        //重新设置认证的相关信息
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, authentication.getCredentials(),collect));

        R r=R.setOk();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(objectMapper.writeValueAsString(r));


    }
}
